/**
 * this file provides the JS for the installer routine
 */

/**
 * Unzip Core JS function
 */
function cleanUpInstall(){

    setInstallProgressInnerHTML("Performing Cleanup - please wait...","add");

    jQuery.getJSON(basepath+"admin/settings/resmania/cleanup", function(data) {
        if (data.success===true){
            setInstallProgressInnerHTML("<img src='" + basepath + "sites/all/modules/ResMania/tick.png' border='0'>","addwithbr");
            setInstallProgressInnerHTML("ResMania Installation is Complete!","add");
        } else {
            setInstallProgressInnerHTML("<img src='" + basepath + "sites/all/modules/ResMania/cross.png' border='0'>","addwithbr");
            alert(data.msg);
        }
    });
}
/**
 * Unzip Core JS function
 */
function checkInstall(){

    setInstallProgressInnerHTML("Checking Installation - please wait...","add");
    
    jQuery.getJSON(basepath+"admin/settings/resmania/checkdb", function(data) {
        if (data.success===true){
            setInstallProgressInnerHTML("<img src='" + basepath + "sites/all/modules/ResMania/tick.png' border='0'>","addwithbr");
            cleanUpInstall();
        } else {
            setInstallProgressInnerHTML("<img src='" + basepath + "sites/all/modules/ResMania/cross.png' border='0'>","addwithbr");
            alert(data.msg);
        }
    });
}
/**
 * Unzip Core JS function
 */
function dbInstall(){

    setInstallProgressInnerHTML("Installing ResMania Database - please wait...","add");

    jQuery.getJSON(basepath+"admin/settings/resmania/dbsetup", function(data) {
        if (data.success===true){
            setInstallProgressInnerHTML("<img src='" + basepath + "sites/all/modules/ResMania/tick.png' border='0'>","addwithbr");
            checkInstall();
        } else {
            setInstallProgressInnerHTML("<img src='" + basepath + "sites/all/modules/ResMania/cross.png' border='0'>","addwithbr");
            alert(data.msg);
        }
    });
}
/**
 * Unzip Core JS function
 */
function unzipCore(){

    setInstallProgressInnerHTML("Unzipping ResMania Core - please wait...","add");

    jQuery.getJSON(basepath+"admin/settings/resmania/unzip", function(data) {
        if (data.success===true){
            setInstallProgressInnerHTML("<img src='" + basepath + "sites/all/modules/ResMania/tick.png' border='0'>","addwithbr");
            dbInstall();
        } else {
            setInstallProgressInnerHTML("<img src='" + basepath + "sites/all/modules/ResMania/cross.png' border='0'>","addwithbr");
            alert(data.msg);
        }
    });
}

/**
 * Download Core JS function
 */
function startDownloadCore(){

    var licenseKey = document.getElementById('licensekey').value;

    if (licenseKey == "") {
        alert("You must enter a valid ResMania License Key in order to access the download server!");
        return;
    }
    document.getElementById('install-licensekey-div').style.display = 'none';

    setInstallProgressInnerHTML("ResMania Core Download Started - please wait...",false);

    document.getElementById('install-button-div').style.display = 'none';

    jQuery.getJSON(basepath+"admin/settings/resmania/download/?key=" + licenseKey, function(data) {
        if (data.success===true){
            setInstallProgressInnerHTML("<img src='" + basepath + "sites/all/modules/ResMania/tick.png' border='0'>","addwithbr");
            unzipCore();
        } else {
            setInstallProgressInnerHTML("<img src='" + basepath + "sites/all/modules/ResMania/cross.png' border='0'>","addwithbr");
            alert(data.msg);
        }
    });
}

/**
 * Returns the inner html of the install-progress-div div
 */
function getInstallProgressInnerHTML(){
     return document.getElementById('install-progress-div').innerHTML;
}

/**
 * Sets the inner html of the install-progress-div div
 */
function setInstallProgressInnerHTML(data, option){

    var current;

    switch(option){
        case "addwithbr":
            current = getInstallProgressInnerHTML();
            document.getElementById('install-progress-div').innerHTML = current + data + "<br/>";
            break;
        case "add":
            current = getInstallProgressInnerHTML();
            document.getElementById('install-progress-div').innerHTML = current + data;
            break;
        default:
            document.getElementById('install-progress-div').innerHTML = data;
    }
}